import random
from typing import List, Dict


def get_participants() -> List[str]:
    """
    获取参与者名单（修正版）
    """
    print("请输入参与者名单，每行一个名字，输入空行结束：")
    participants = []
    while True:
        name = input().strip()
        if not name:
            break
        # 分割一行中的多个名字（兼容两种输入方式）
        names = name.split()
        participants.extend(names)

    # 去除空名字
    participants = [name for name in participants if name]
    return participants


def set_lottery_rules() -> Dict:
    """
    设置抽奖规则（修正版）
    """
    rules = {}
    print("\n请设置抽奖规则：")

    while True:
        try:
            prize_count = int(input("请输入奖项数量："))
            if prize_count > 0:
                rules["prize_count"] = prize_count
                break
            print("奖项数量必须大于0！")
        except ValueError:
            print("请输入有效的数字！")

    while True:
        allow_repeat = input("是否允许重复获奖？(y/n): ").lower()
        if allow_repeat in ('y', 'n'):
            rules["allow_repeat"] = allow_repeat == 'y'
            break
        print("请输入 y 或 n！")

    return rules


def draw_winners(participants: List[str], rules: Dict) -> List[str]:
    """
    随机抽取获奖者（修正版）
    """
    prize_count = rules["prize_count"]
    allow_repeat = rules["allow_repeat"]
    winners = []

    if not participants:
        print("警告：参与者名单为空！")
        return winners

    # 不允许重复且奖项数超过参与者人数时，调整奖项数
    if not allow_repeat and prize_count > len(participants):
        print(f"警告：参与者不足{prize_count}人，已调整为{len(participants)}人获奖")
        prize_count = len(participants)

    pool = participants.copy()
    for _ in range(prize_count):
        if not pool:
            break
        winner = random.choice(pool)
        winners.append(winner)
        if not allow_repeat:
            pool.remove(winner)

    return winners


def display_results(winners: List[str]) -> None:
    """
    显示获奖结果（修正版）
    """
    if not winners:
        print("\n没有抽取到获奖者！")
        return

    print("\n=== 抽奖结果 ===")
    for i, winner in enumerate(winners, 1):
        print(f"{i}. {winner}")
    print("===============")


def main():
    """
    主函数（修正版）
    """
    print("=== 简易抽奖系统 ===")

    # 获取参与者名单
    participants = get_participants()
    if not participants:
        print("错误：没有输入任何参与者！")
        return

    print(f"\n当前参与者名单：{', '.join(participants)}")
    print(f"参与者人数：{len(participants)}人")

    # 设置抽奖规则
    rules = set_lottery_rules()

    # 抽取获奖者
    winners = draw_winners(participants, rules)

    # 显示结果
    display_results(winners)


if __name__ == "__main__":
    main()